package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.annotation.WxCartAnnotation;
import com.cskaoyan.market.db.domain.MarketCart;
import com.cskaoyan.market.service.WxCartService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.WxCartCheckoutVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    WxCartService wxCartService;

    @PostMapping("update")
    public Object update(@RequestBody MarketCart marketCart){
        int i = wxCartService.update(marketCart);
        if(i==0){
            Object fail = ResponseUtil.fail();
            return fail;
        }
        Object ok = ResponseUtil.ok();
        return ok;
    }
    @WxCartAnnotation
    @PostMapping("delete")
    public Object delete(@RequestBody Map<String, List<Integer>> map){
        HashMap<String, Object> data = new HashMap<>();
        List<Integer> ids = map.get("productIds");
        List<MarketCart> marketCartList = wxCartService.delete(ids);
        //返回购物车中剩余商品，暂不判空
        //TODO 切面增强
//        checkedGoodsAmount: 2443
//        checkedGoodsCount: 7
//        goodsAmount: 2443
//        goodsCount: 7
//        HashMap<String, Object> cartTotal = new HashMap<>();
//        cartTotal.put("checkedGoodsAmount",wxCartService.checkedGoodsAmount());
//        cartTotal.put("checkedGoodsCount",wxCartService.checkedGoodsCount());
//        cartTotal.put("goodsAmount",wxCartService.goodsAmount());
//        cartTotal.put("goodsCount",wxCartService.goodsCount());
        data.put("cartList",marketCartList);
//        data.put("cartTotal",cartTotal);
//        Object ok = ResponseUtil.ok(data);
        return data;
    }

    @GetMapping("goodscount")
    public Object goodsCount(){
        int nums = wxCartService.goodsCount();
        Object ok = ResponseUtil.ok(nums);
        return ok;
    }

    @PostMapping("add")
    public Object add(@RequestBody MarketCart marketCart){
        // 自建sql
        int i = wxCartService.add(marketCart);
        if(i==0){
            Object fail = ResponseUtil.fail();
            return fail;
        }
        int data = wxCartService.goodsCount();
        Object ok = ResponseUtil.ok(data);
        return ok;
    }
    @WxCartAnnotation
    @GetMapping("index")
    public Object index(){
        HashMap<String, Object> data = new HashMap<>();
        List<MarketCart> marketCartList = wxCartService.index();
        //返回购物车中剩余商品，暂不判空
        //TODO 切面增强
//        checkedGoodsAmount: 2443
//        checkedGoodsCount: 7
//        goodsAmount: 2443
//        goodsCount: 7
//        HashMap<String, Object> cartTotal = new HashMap<>();
//        cartTotal.put("checkedGoodsAmount",wxCartService.checkedGoodsAmount());
//        cartTotal.put("checkedGoodsCount",wxCartService.checkedGoodsCount());
//        cartTotal.put("goodsAmount",wxCartService.goodsAmount());
//        cartTotal.put("goodsCount",wxCartService.goodsCount());
        data.put("cartList",marketCartList);
//        data.put("cartTotal",cartTotal);
//        Object ok = ResponseUtil.ok(data);
        return data;
    }
    @WxCartAnnotation
    @PostMapping("checked")
    public Object checked(@RequestBody Map<String,Object> map){
        int isChecked = (int) map.get("isChecked");
        List<Integer> productIds = (List<Integer>) map.get("productIds");
        HashMap<String, Object> data = new HashMap<>();
        List<MarketCart> marketCartList = wxCartService.checked(isChecked,productIds);
        //TODO 切面增强
//        checkedGoodsAmount: 2443
//        checkedGoodsCount: 7
//        goodsAmount: 2443
//        goodsCount: 7
//        HashMap<String, Object> cartTotal = new HashMap<>();
//        cartTotal.put("checkedGoodsAmount",wxCartService.checkedGoodsAmount());
//        cartTotal.put("checkedGoodsCount",wxCartService.checkedGoodsCount());
//        cartTotal.put("goodsAmount",wxCartService.goodsAmount());
//        cartTotal.put("goodsCount",wxCartService.goodsCount());
        data.put("cartList",marketCartList);
//        data.put("cartTotal",cartTotal);
//        Object ok = ResponseUtil.ok(data);
        return data;
    }

    @PostMapping("fastadd")
    public Object fastAdd(@RequestBody MarketCart marketCart){
        int data = wxCartService.fastAdd(marketCart);
        Object ok = ResponseUtil.ok(data);
        return ok;
    }
    @GetMapping("checkout")
    public Object checkout(WxCartCheckoutVo vo){
        //Integer cartId,Integer addressId,Integer couponId,Integer userCouponId,Integer grouponRulesId
        WxCartCheckoutVo wxCartCheckoutVo = wxCartService.checkout(vo);
        Object ok = ResponseUtil.ok(wxCartCheckoutVo);
        return ok;
    }
}
